package com.blog.web.manage;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.blog.pojo.Article;
import com.blog.pojo.Cate;
import com.blog.service.ArticleService;


/** 
 * @author ant 
 * @date 创建时间：2017年8月4日 下午4:10:21  
 */
@Controller("manageArticle")
@RequestMapping("/manage")
public class ArticleViewController {
	
	@Autowired
	private ArticleService articleService;
	
	private final String ARTICLE = "manage/article";
	
	@RequestMapping("/view/cateList")
	public String goToCate() {
		return ARTICLE+"/cateList";	
	}
	
	@RequestMapping("/view/articleList")
	public String goToArticle() {
		return ARTICLE+"/articleList";	
	}
	
	/**
	 * @Description:  跳转到分类编辑页面
	 * @param articleId
	 * @param act 编辑状态：add为添加 ；edit为修改
	 * @param model
	 * @return String
	 */
	@RequestMapping(value="/cateEdit",method=RequestMethod.GET)
	public String goToEditCate(Integer cateId,String act,Model model) {
		if (StringUtils.equals(act, "edit")) {
			Cate cate = articleService.getCateByCateId(cateId);
			model.addAttribute("cate", cate);
			model.addAttribute("title", "更新分类");
		} else {
			Cate cate = new Cate();
			model.addAttribute("cate", cate);
			model.addAttribute("title", "创建分类");
		}
		model.addAttribute("act", act);		
		return ARTICLE+"/editCate";
    }
	
	@RequestMapping("/view/addArticle")
	public String goToAddArticle(Model model) {
		List<Cate> cateList = articleService.listAllCate(null);
		model.addAttribute("cateList", cateList);
		return ARTICLE+"/addArticle";	
	}
	
	@RequestMapping("/article/articleEdit")
	public String goToEditArticle(Model model,Integer articleId) {
		Article article = articleService.getArticleByArticleId(articleId);
		List<Cate> cateList = articleService.listAllCate(null);
		model.addAttribute("cateList", cateList);
		model.addAttribute("article", article);
		return ARTICLE+"/editArticle";	
	}

}
